﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using EnvDTE;
using Comet.Infrastructure;

namespace Comet.ViewModel
{
    /// <summary>   
    ///     Data members view model. 
    /// </summary>
    /// <remarks>   
    ///     Simon, 20.02.2011. 
    /// </remarks>
    public class DataMembersViewModel
    {
        #region Fields

        private CodeClass codeClass;
        private ObservableCollection<DataMemberViewModel> dataMembers = new ObservableCollection<DataMemberViewModel>();

        #endregion Fields 

        #region Constructors

        /// <summary>   
        ///     Creates a new instance of the DataMembersViewModel class. 
        /// </summary>
        /// <remarks>   
        ///     Simon, 20.02.2011. 
        /// </remarks>
        /// <exception cref="ArgumentNullException">    Thrown when one or more required arguments are
        ///                                             null. </exception>
        /// <param name="codeClass">    The code class. </param>
        public DataMembersViewModel(CodeClass codeClass)
        {
            if (codeClass == null)
            {
                throw new ArgumentNullException("codeClass");
            }

            this.codeClass = codeClass;
        }

        #endregion Constructors 

        #region Properties

        /// <summary>   
        ///     Gets the data members. 
        /// </summary>
        /// <value> 
        ///     The data members. 
        /// </value>
        public ObservableCollection<DataMemberViewModel> DataMembers
        {
        	get 
            { 
                return dataMembers; 
            }
        }

        /// <summary>   
        ///     Gets or sets a value indicating whether the changes were accepted. 
        /// </summary>
        /// <value> 
        ///     true if changes accepted, false if not. 
        /// </value>
        public bool ChangesAccepted
        {
            get;
            set;
        }

        #endregion

        #region Initialization

        // Public Methods (1) 

        /// <summary>   
        ///     Initialiaze data members. 
        /// </summary>
        /// <remarks>   
        ///     Simon, 20.02.2011. 
        /// </remarks>
        /// <param name="codeClass">    The code class. </param>
        public void InitialiazeDataMembers()
        {
            if (codeClass != null)
            {
                dataMembers.Clear();
                ExtractedDataMembers extractedDataMembers = CometUtils.ExtractSettableNonStaticDataMembers(codeClass);

                foreach (var dataMember in extractedDataMembers.Fields)
                {
                    dataMembers.Add(new DataMemberViewModel(dataMember));
                }

                foreach (var dataMember in extractedDataMembers.Properties)
                {
                    dataMembers.Add(new DataMemberViewModel(dataMember));
                }
            }
        }

        #endregion
    }
}
